package com.example.myapplication.adapter;


import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.R;
import com.example.myapplication.util.ItemMoveCallback;

import java.util.Collections;
import java.util.List;

public class NewsSortAdapter extends RecyclerView.Adapter<NewsSortAdapter.ViewHolder> implements ItemMoveCallback {
    private final String TAG = "NewsSortAdapter";
    private List<String> newsSortList;
    private Context context;
    public NewsSortAdapter(Context context, List<String> newsSortList){
        this.newsSortList = newsSortList;
        this.context = context;
    }


    private NewsSortAdapter.OnItemClickListener onItemClickListener;



    @Override
    public void onMove(int fromPosition, int toPosition) {
//        Log.d(TAG, "onMove: 移动前的数据");
//        for (int i = 0; i < newsSortList.size(); i++) {
//            Log.d(TAG, "onMove: 索引" + i + " -> " + newsSortList.get(i));
//        }
        //将原位置元素添加到指定位置，指定位置后的元素依次退一，和动画效果保持一致
        // 移除原位置元素
        String item = newsSortList.remove(fromPosition);
        // 插入到新位置
        newsSortList.add(toPosition, item);
        //通知数据移动
        notifyItemMoved(fromPosition, toPosition);
//        Log.d(TAG, "onMove: 移动后的数据");
//        for (int i = 0; i < newsSortList.size(); i++) {
//            Log.d(TAG, "onMove: 索引" + i + " -> " + newsSortList.get(i));
//        }
    }

    public interface OnItemClickListener{
        void onItemClick(View view,int position);
    }

    public void setOnItemClickListener(NewsSortAdapter.OnItemClickListener listener){
        this.onItemClickListener = listener;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_news_sort,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.tvSort.setText(newsSortList.get(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClick(v,holder.getAdapterPosition());
            }
        });

    }

    @Override
    public int getItemCount() {
        return newsSortList.size();
    }


    //刷新数据
    public void setData(List<String>  datas){
        this.newsSortList=datas;
        notifyDataSetChanged();
    }

    public List<String> getData(){
        return newsSortList;
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
         TextView tvSort;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvSort = (TextView) itemView.findViewById(R.id.tv_sort);

        }


    }
}
